package com.generator;

import cn.hutool.core.util.StrUtil;
import com.generator.util.CommentUtil;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fengz
 * @version v1
 * @className ToolPlugin
 * @description 数据库访问层
 * @site glp.hcdlearning.com
 * @company 合得软件（上海）有限公司
 * @create 2019-07-16 11:45
 */
public class DaoPlugin extends PluginAdapter {
    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        List<GeneratedJavaFile> list = new ArrayList<GeneratedJavaFile>();
        List<TopLevelClass> units = new ArrayList<TopLevelClass>();
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                StrUtil.format("com.demo.web.dao.{}Dao",introspectedTable.getFullyQualifiedTable().getDomainObjectName())
        );
        Interface inter = new Interface(type);
        inter.setVisibility(JavaVisibility.PUBLIC);
        inter.addImportedType(new FullyQualifiedJavaType(
                StrUtil.format("com.demo.web.generator.entity.{}",introspectedTable.getFullyQualifiedTable().getDomainObjectName())
        ));
//        inter.addImportedType(new FullyQualifiedJavaType(
//                StrUtil.format("com.baomidou.mybatisplus.core.mapper.BaseMapper<{}>",introspectedTable.getFullyQualifiedTable().getDomainObjectName())
//        ));
//        inter.addSuperInterface(new FullyQualifiedJavaType(
//                StrUtil.format("BaseMapper<{}>",introspectedTable.getFullyQualifiedTable().getDomainObjectName())
//        ));
        CommentUtil.addModelClassComment(inter,inter.getType().getShortName(),introspectedTable.getFullyQualifiedTable().getFullyQualifiedTableNameAtRuntime());
        list.add(new GeneratedJavaFile(inter, getContext().getJavaModelGeneratorConfiguration().getTargetProject(), getContext().getJavaFormatter()));
        return list;
    }

}
